package com.lbs.jiepiaisi.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.adapter.NewsViewPagerAdapter;
import com.lbs.jiepiaisi.adapter.WorkOrderViewPagerAdapter;
import com.lbs.jiepiaisi.base.BaseActivity;
import com.lbs.jiepiaisi.base.BaseFragmentActivity;
import com.lbs.jiepiaisi.bean.LoginPhoneResultBean;
import com.lbs.jiepiaisi.bean.WorkOrderListBean;
import com.lbs.jiepiaisi.biz.LocationBiz;
import com.lbs.jiepiaisi.biz.LocationUserBiz;

/**
 *
 * 时间：2018/1/23 15:57
 * 作用：工单界面
 */
public class WorkOrderActivity extends BaseFragmentActivity implements View.OnClickListener,AMapLocationListener{
    LoginPhoneResultBean mLoginPhoneResultBean;

    private WorkOrderViewPagerAdapter mWorkOrderViewPagerAdapter;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    private ImageButton mTitleBack;

    //声明mlocationClient对象
    public AMapLocationClient mlocationClient;
    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;

    @Override
    protected void setView() {
        super.setView();
        setContentView(R.layout.activity_work_order);
    }

    @Override
    protected void getExtras() {
        mLoginPhoneResultBean = (LoginPhoneResultBean)getIntent().getSerializableExtra("phoneLogin");
    }

    @Override
    protected void findView() {
        mViewPager = (ViewPager) findViewById(R.id.vp_viewpager);
        mTabLayout = (TabLayout) findViewById(R.id.tl_tabs);
        mTitleBack = (ImageButton) findViewById(R.id.title_but_left);


        mWorkOrderViewPagerAdapter = new WorkOrderViewPagerAdapter(getSupportFragmentManager(),mLoginPhoneResultBean.getResult().getInstallId());
        mViewPager.setAdapter(mWorkOrderViewPagerAdapter);
        //Viewpager和Tablayout进行关联
        mTabLayout.setupWithViewPager(mViewPager);
        //用于多TAB，Tablayout不可以滚动
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);

        initLocation();
    }

    @Override
    protected void setListener() {
        mTitleBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_but_left:
                finish();
                break;
        }
    }

    public void initLocation(){
        mlocationClient = new AMapLocationClient(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mlocationClient.setLocationListener(this);
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setOnceLocation(true);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
        mlocationClient.startLocation();
    }
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        new LocationUserBiz().updateBiz(mLoginPhoneResultBean.getResult().getInstallId(),aMapLocation.getLatitude()+"",aMapLocation.getLongitude()+"");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mlocationClient.stopLocation();
    }
}
